﻿var me = this,
    column = view.ownerCt.getColumnManager().getHeaderAtIndex(cellIndex);
var vm = this.lookupViewModel();
var isInquiryCall = vm.get('NebulogyForm.isInquiryCall') || 0;
var viewStatus = this.lookupController().analysis().status;
var expenseType = ref.ExpenseType.getValue();

if (viewStatus == 'launch' || viewStatus == 'resubmit') {
    if (column.dataIndex == 'Code' && isInquiryCall == 0) {
        //统计VendorInformation里面的code.
        var codeList = [];
        var storeData = ref.datagrid1.getStore().getData().items;

        Ext.each(storeData, function (r) {
            codeList.push(r.get('Code'));
        });

        //弹出窗
        var window1 = Ext.create('Ext.window.Window', {
            title: 'Code Choose',
            height: 560,
            width: 650,
            modal: true,
            // closeAction: 'hide',
            layout: {
                type: 'float',
                columns: 1
            },
            buttons: [{
                xtype: "button",
                text: "Confirm",
                handler: function () {
                    var win = this.up("window"),
                        refs = win.getReferences();
                    //赋值 

                    var selectRecords = refs.grid.getSelection();
                    if (selectRecords.length > 0) {
                        setRecord(selectRecords);
                    }
                }
            }, {
                xtype: "button",
                text: "Cancel",
                handler: function () {
                    this.up("window").close();
                }
            }],
            //主视图内部控件
            referenceHolder: true,
            items: {
                xtype: 'paigrid',
                reference: 'grid',
                flex: 10,
                enableAdd: false,
                enableDel: false,
                enableSearch: false,
                pagingConfigs: {
                    displayInfo: true
                },
                enablePaging: true,
                dockedItems: [{
                    xtype: 'toolbar',
                    dock: 'top',
                    items: [{
                        xtype: 'textfield',
                        name: 'searchField',
                        blankText: 'Code/Name/Chinese Name',
                        width: 300
                    }, '->', {
                        xtype: 'button',
                        text: 'Search',
                        handler: function () {
                            var store = this.up('paigrid').getStore();
                            store.load();
                        }
                    }]
                }],
                columns: [{
                    text: 'Code',
                    dataIndex: 'vendorCode',
                    flex: 1
                }, {
                    text: 'Name',
                    dataIndex: 'vendorName',
                    flex: 1
                }, {
                    text: 'Chinese Name',
                    dataIndex: 'chineseName',
                    flex: 1
                }],
                store: {
                    autoLoad: true,
                    pageSize: 10,
                    fields: ['vendorCode', 'vendorName', 'chineseName'],
                    proxy: {
                        type: 'pai.rest',
                        url: 'last/jlr_budgetVendorCode/customQuery',
                        ns: PAI.application.objectServerUrl,
                        postRead: true,
                        timeout: 100000000,
                        extraParams: {
                            vendorCode: ""
                        },
                        reader: {
                            rootProperty: 'data',
                        }
                    },
                    listeners: {
                        beforeload: function (store) {
                            var searchField = window1.down('textfield[name=searchField]').getValue();
                            var filter = "";
                            if (searchField) {
                                filter = Ext.String.format("vendorName like '%{0}%' or vendorCode like '%{0}%' or chineseName like '%{0}%'", searchField);
                            }
                            store.proxy.setExtraParams({
                                fields: [
                                    { name: "vendorName" },
                                    { name: "vendorCode" },
                                    { name: "chineseName" }
                                ],
                                filter: filter
                            });
                        }
                    }
                },
                listeners: {
                    itemdblclick: function (grid, record) {
                        setRecord([record]);
                    }
                }
            },
            listeners: {
                close: function () {
                    Ext.getBody().unmask();
                }
            }
        }).show();

        function setRecord(records) {
            if (Ext.Array.contains(codeList, records[0].get("vendorCode"))) {
                Ext.Msg.alert('System Tips', 'Code already exists !');
            } else {
                //先保存最原始的数据
                var oldCode = record.data.Code;
                record.data.Code = records[0].get("vendorCode");
                record.data.Name = records[0].get("vendorName");
                record.commit();

                var editor = expenseType == '4' ? ref.datagrid7.getColumns()[4].getEditor() : ref.datagrid2.getColumns()[2].getEditor();

                var list = Ext.pluck(ref.datagrid1.getStore().data.items, 'data');
                var arr = [];
                Ext.each(list, function (item) {
                    arr.push({
                        code: item.Code,
                        name: item.Code,
                        _name: item.Name
                    });
                });

                editor.getStore().loadData(arr);
                //重新填充数据 需要更新采购Item里面对应的Code.

                var purStore = expenseType == '4' ? ref.datagrid7.getStore() : ref.datagrid2.getStore();

                Ext.each(purStore.data.items, function (r) {
                    if (r.get('VendorCode') == oldCode) {
                        r.data.VendorCode = record.data.Code;
                        r.data.VendorName = record.data.Name;
                        r.commit();
                    }
                });

                window1.close();
            }
        }
    }
}